//
//  LogAsErrorEvent v 2.3 - logAs package
//  Russell Lowke, December 18th 2011
// 
//  Copyright (c) 2009-2011 Lowke Media
//  see http://www.lowkemedia.com for more information
//  see http://code.google.com/p/lowke/ for code repository
// 
//  Permission is hereby granted, free of charge, to any person obtaining a 
//  copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the 
//  Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in 
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER  
//  DEALINGS IN THE SOFTWARE. 
//
//

package com.lowke.logAs.event
{   
    import flash.events.Event;
    
    /**
     * @author Russell Lowke
     */
    public class LogAsEvent extends Event 
    {   
        // event name
        public static const LOG_AS_EVENT:String = "logAsEvent";
        
        // errorLevel types
        public static const SEVERE:uint  = 1;
        public static const WARNING:uint = 2;
        public static const INFO:uint    = 3;
        public static const PRINT:uint   = 4;       // PRINT is a less spammy version of DEBUG for one-off testing
        public static const DEBUG:uint   = 5;
        
        private static const DIGITS_IN_PREFIX:uint = 3;
        private static const DIGITS_IN_NUMBER:uint = 2;
        
        
        private var _message:String;        // message being logged
        private var _level:uint;            // log level of message
        private var _id:String;             // id of log message
        private var _stackTrace:String;     // stack trace for log
        
        public function LogAsEvent(type:String, 
                                   message:String, 
                                   level:uint, 
                                   id:String = null,
                                   stackTrace:String = null) 
        {
            super(type);
            
            _message = message;
            _level = level;
            _id = id;
            _stackTrace = stackTrace;
        }
        
        public override function clone():Event 
        {
            return new LogAsEvent(type, message, level, id);
        }
        
        public override function toString():String
        {
            if (_stackTrace) 
            {
                // _stackTrace has the error's toString(_level, _message, _id) inside it
                return _stackTrace;
            } 
            else 
            {
                return LogAsEvent.toString(_level, _message, _id);
            }
        }
        
        //
        // accessors and mutators
        
        public function get message():String        { return _message; }
        public function get level():uint            { return _level; }
        public function get id():String             { return _id; }
        public function get stackTrace():String     { return _stackTrace; }
        
        
        
        // log identifiers are comprised of a three digit prefix 
        //  followed by a two or more digit number code
        //  e.g. "LDR02" 
        
        public function get idPrefix():String
        {
            if (! _id) 
            {
                return "";  
            }
            
            // remove the numbers
            var string:String = _id.substring(0, DIGITS_IN_PREFIX);
            return string; 
        }
        
        // Strips out any extra characters added to log id.
        //  Some errors, such as font related errors, 
        //  might add information such as the name of the font 
        //  to the error id
        public function get idBasic():String
        {
            if (! _id) 
            {
                return "";  
            }
            
            var string:String = _id.substring(0, DIGITS_IN_PREFIX + DIGITS_IN_NUMBER);
            return string; 
        }
        
        // id numbers change, identifying an log by number is discouraged
        public function get idNumber():int
        {
            if (! _id) 
            {
                return -1;
            }
            
            // extract the numbers
            var string:String = _id.substring(DIGITS_IN_PREFIX, DIGITS_IN_PREFIX + DIGITS_IN_NUMBER);
            var idNumber:uint = uint(string);
            return idNumber; 
        }
        
        // create string from general log parameters
        public static function toString(level:uint,
                                        message:String,
                                        id:String = null,
                                        stackTrace:String = null):String
        {   
            var str:String;
            switch (level) 
            {
                case SEVERE:    str = "SEVERE";     break;
                case WARNING:   str = "WARNING";    break;
                case INFO:      str = "INFO";       break;
                case PRINT:     str = "PRINT";      break;
                case DEBUG:     str = "DEBUG";      break;
            }
            
            return str + (id ? " #" + id : "") + ": " + message;
        }
    }
}